package com.lanou.horo.zhuizhui.fragment;


import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.horo.zhuizhui.activity.LogInActivity;
import com.lanou.horo.zhuizhui.activity.UserInformationActivity;
import com.lanou.horo.zhuizhui.utility.customImage.RoundedImageView;
import com.lanou.horo.zhuizhui.activity.SettingActivity;
import com.lanou.horo.zhuizhui.activity.UserMessageActivity;
import com.lanou.horo.zhuizhui.adapter.PersonalPageTitleAdapter;
import com.lanou.horo.zhuizhui.R;
import com.lanou.horo.zhuizhui.utility.imageLoader.BaseApplication;


public class PersonalPageFragment extends Fragment implements View.OnClickListener {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ImageView message, setting,backgroundView;
    private RoundedImageView login;
    private TextView userNameTv, userSignatureTextView;


    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_page_personal, null);
        tabLayout = (TabLayout) view.findViewById(R.id.personalTabLayout);
        viewPager = (ViewPager) view.findViewById(R.id.personalViewPager);
        backgroundView = (ImageView) view.findViewById(R.id.backgroundView);
        login = (RoundedImageView) view.findViewById(R.id.userPhotoImg);
        login.setOnClickListener(this);
        message = (ImageView) view.findViewById(R.id.userMessage);
        message.setOnClickListener(this);
        setting = (ImageView) view.findViewById(R.id.userSetting);
        setting.setOnClickListener(this);
        userNameTv = (TextView) view.findViewById(R.id.userNameTv);
        userSignatureTextView = (TextView) view.findViewById(R.id.userSignatureTextView);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();

    }

    private void initData() {
        PersonalPageTitleAdapter adapter = new PersonalPageTitleAdapter(getFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (BaseApplication.islogin()) {
            if (BaseApplication.getUser().getUserPhoto() != null){

            login.setImageBitmap(Bytes2Bimap(BaseApplication.getUser().getUserPhoto()));
            }
            if (BaseApplication.getUser().getUserBackground() != null){
            backgroundView.setImageBitmap(Bytes2Bimap(BaseApplication.getUser().getUserBackground()));

            }
            userNameTv.setText(BaseApplication.getUser().getName());
            userSignatureTextView.setText(BaseApplication.getUser().getSignature());
        } else {
            backgroundView.setImageResource(R.color.colorPrimary);
            login.setImageResource(R.mipmap.user_icon_big);
            userNameTv.setText("用户名");
            userSignatureTextView.setText("签名");
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.userPhotoImg:
                Intent logIntent = new Intent();
                if (BaseApplication.islogin()) {
                    logIntent.setClass(getActivity(), UserInformationActivity.class);
                } else {
                    logIntent.setClass(getActivity(), LogInActivity.class);
                }
                startActivity(logIntent);
                break;
            case R.id.userMessage:
                Intent messageIntent = new Intent();
                if (BaseApplication.islogin()) {
                    messageIntent.setClass(getActivity(), UserMessageActivity.class);
                } else {
                    messageIntent.setClass(getActivity(), LogInActivity.class);
                }
                startActivity(messageIntent);
                break;
            case R.id.userSetting:
                Intent setIntent = new Intent(getActivity(), SettingActivity.class);
                startActivity(setIntent);
                break;
        }
    }

    public Bitmap Bytes2Bimap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }
}
